using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace MicroMsg.Manager
{
	public class ImageCacheMgr
	{
		private const string TAG = "ImageCacheMgr";
		private static Dictionary<string, ImageCache> mapImageCache = new Dictionary<string, ImageCache>();
		public static void init()
		{
		}
		public static void uninit()
		{
		}
		public static BitmapImage loadFromRes(string imgPath)
		{
			if (string.IsNullOrEmpty(imgPath))
			{
				return null;
			}
			if (imgPath.StartsWith("/"))
			{
				imgPath = imgPath.Substring(1);
			}
			Uri uri = new Uri(imgPath, 2);
			StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
			BitmapImage result;
			try
			{
				BitmapImage bitmapImage = new BitmapImage();
				bitmapImage.SetSource(resourceStream.get_Stream());
				result = bitmapImage;
			}
			catch (Exception ex)
			{
				Log.smethod_1("ImageCacheMgr", "decode image faile ! " + ex);
				throw;
			}
			return result;
		}
		public static BitmapImage loadFromFile(string imgPath)
		{
			if (string.IsNullOrEmpty(imgPath))
			{
				return null;
			}
			byte[] array = null;
			try
			{
				array = StorageIO.readFromFile(imgPath);
				BitmapImage result;
				if (array == null)
				{
					result = null;
					return result;
				}
				BitmapImage bitmapImage = new BitmapImage();
				bitmapImage.SetSource(new MemoryStream(array));
				result = bitmapImage;
				return result;
			}
			catch (Exception ex)
			{
				Log.smethod_1("ImageCacheMgr", string.Concat(new object[]
				{
					"loadImage decode imgfile fail  ",
					imgPath,
					" ",
					ex
				}));
				if (array.Length >= 4)
				{
					Log.smethod_1("ImageCacheMgr", "loadImage decode imgfile head 0x" + Util.toHexString(array, 4));
				}
			}
			return null;
		}
		public static BitmapImage loadFromBuf(byte[] imageData)
		{
			if (imageData != null && imageData.Length > 0)
			{
				try
				{
					BitmapImage bitmapImage = new BitmapImage();
					bitmapImage.SetSource(new MemoryStream(imageData));
					return bitmapImage;
				}
				catch (Exception ex)
				{
					Log.smethod_1("ImageCacheMgr", "loadImage decode imgbuf fail  " + ex);
					if (imageData.Length >= 4)
					{
						Log.smethod_1("ImageCacheMgr", "loadImage decode imgfile head 0x" + Util.toHexString(imageData, 4));
					}
				}
				return null;
			}
			return null;
		}
		public static BitmapImage loadResImage(string imgPath)
		{
			return ImageCacheMgr.loadToCache(imgPath, ImageCacheType.AppRes, imgPath, ImageDataType.const_0);
		}
		public static BitmapImage loadImage(string imgPath, ImageCacheType type)
		{
			if (string.IsNullOrEmpty(imgPath))
			{
				return null;
			}
			ImageDataType data_type = ImageDataType.file;
			if (imgPath.StartsWith("/Resource") || imgPath.StartsWith("Resource"))
			{
				data_type = ImageDataType.const_0;
			}
			return ImageCacheMgr.loadToCache(imgPath, type, imgPath, data_type);
		}
		public static BitmapImage loadToCache(string imgUri, ImageCacheType cacheType, object image_data, ImageDataType data_type)
		{
			ImageCache cache = ImageCacheMgr.getCache(imgUri);
			if (cache != null)
			{
				return cache.bitmap;
			}
			if (string.IsNullOrEmpty(imgUri) || image_data == null)
			{
				return null;
			}
			BitmapImage bitmapImage;
			switch (data_type)
			{
			case ImageDataType.const_0:
				bitmapImage = ImageCacheMgr.loadFromRes(image_data as string);
				break;
			case ImageDataType.file:
				bitmapImage = ImageCacheMgr.loadFromFile(image_data as string);
				break;
			case ImageDataType.buffer:
				bitmapImage = ImageCacheMgr.loadFromBuf(image_data as byte[]);
				break;
			default:
				return null;
			}
			if (bitmapImage == null)
			{
				return null;
			}
			return ImageCacheMgr.addCache(imgUri, cacheType, bitmapImage);
		}
		public static bool isCacheExist(string imgUri)
		{
			if (string.IsNullOrEmpty(imgUri))
			{
				return false;
			}
			bool result;
			lock (ImageCacheMgr.mapImageCache)
			{
				result = ImageCacheMgr.mapImageCache.ContainsKey(imgUri);
			}
			return result;
		}
		public static ImageCache getCache(string imgUri)
		{
			if (string.IsNullOrEmpty(imgUri))
			{
				return null;
			}
			ImageCache result;
			lock (ImageCacheMgr.mapImageCache)
			{
				if (!ImageCacheMgr.mapImageCache.ContainsKey(imgUri))
				{
					result = null;
				}
				else
				{
					result = ImageCacheMgr.mapImageCache.get_Item(imgUri);
				}
			}
			return result;
		}
		public static BitmapImage addCache(string imgUri, ImageCacheType imageType, BitmapImage image)
		{
			if (!string.IsNullOrEmpty(imgUri) && image != null)
			{
				lock (ImageCacheMgr.mapImageCache)
				{
					ImageCache imageCache = new ImageCache();
					imageCache.string_0 = imgUri;
					imageCache.type = imageType;
					imageCache.bitmap = image;
					ImageCacheMgr.mapImageCache.set_Item(imgUri, imageCache);
				}
				return image;
			}
			return null;
		}
		public static void delCache(string imgUri)
		{
			if (string.IsNullOrEmpty(imgUri))
			{
				return;
			}
			lock (ImageCacheMgr.mapImageCache)
			{
				if (ImageCacheMgr.mapImageCache.ContainsKey(imgUri))
				{
					ImageCacheMgr.mapImageCache.get_Item(imgUri).bitmap = null;
					ImageCacheMgr.mapImageCache.Remove(imgUri);
				}
			}
		}
		public static void delCacheByType(ImageCacheType imageType)
		{
			lock (ImageCacheMgr.mapImageCache)
			{
				IEnumerable<string> enumerable = Enumerable.Select<KeyValuePair<string, ImageCache>, string>(Enumerable.Where<KeyValuePair<string, ImageCache>>(ImageCacheMgr.mapImageCache, (KeyValuePair<string, ImageCache> keyValuePair_0) => keyValuePair_0.get_Value().type == imageType), (KeyValuePair<string, ImageCache> keyValuePair_0) => keyValuePair_0.get_Key());
				if (Enumerable.Count<string>(enumerable) > 0)
				{
					List<string> list = Enumerable.ToList<string>(enumerable);
					using (List<string>.Enumerator enumerator = list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							string current = enumerator.get_Current();
							ImageCacheMgr.mapImageCache.get_Item(current).bitmap = null;
							ImageCacheMgr.mapImageCache.Remove(current);
						}
					}
				}
			}
		}
	}
}
